package com.hiwan.unions.activity.main.fragment.base;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hiwan.unions.R;
import com.hiwan.unions.application.ConstantsValues;
import com.hiwan.unions.view.PullToRefreshLayout;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.ViewStatus;
import com.yanzhenjie.nohttp.Logger;

import me.yokeyword.fragmentation.SupportFragment;

/**
 * Created by haorui on 16/8/11.
 * 无MVP的Fragment基类
 */

public abstract class RootFragment extends SupportFragment {
    protected SharedPreferences userInfo;
    protected SharedPreferences userInfoC;
    protected Activity mActivity;
    protected Context mContext;
    // 内容布局
    protected PullToRefreshLayout refreshLayout;
    protected View mContentView;
    /**
     * 默认为不需要显示加载动画
     * 如需要显示动画，需要在getLayout()中，将 showLoadingView = true
     */
    protected View view;
    protected boolean canLoadMore = true;//是否启用加载更多
    protected boolean canRefresh = true;//是否启用下拉刷新
    private TextView empty_text;

    @Override
    public void onAttach(Context context) {
        mActivity = (Activity) context;
        mContext = context;
        super.onAttach(context);
    }

    protected abstract void initData();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_root, null);
        mContentView = LayoutInflater.from(mActivity).inflate(getLayout(), null, false);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mContentView.setLayoutParams(params);
        refreshLayout = (PullToRefreshLayout) view.findViewById(R.id.refreshLayout_fragment);
        refreshLayout.addView(mContentView);
        return view;
    }


    public <T extends View> T findView(int viewId) {
        return (T) refreshLayout.findViewById(viewId);
    }

    public <T extends View> T findView(View view, int viewId) {
        return (T) view.findViewById(viewId);
    }


    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        userInfo = getActivity().getSharedPreferences(ConstantsValues.INIT_USER_INFO,
                Context.MODE_PRIVATE);
        userInfoC = getActivity().getSharedPreferences(ConstantsValues.APP,
                Context.MODE_PRIVATE);
        Interface();
        initData();
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.i("Unions ", " 是否执行  ");
        refreshLayout.setCanLoadMore(canLoadMore);
        refreshLayout.setCancanRefresh(canRefresh);
        refreshLayout.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                DataRefresh();
            }

            @Override
            public void loadMore() {
                DataRoadMore();
            }
        });
    }

    protected <T extends View> T getView(int id) {
        return (T) getView().findViewById(id);
    }

    protected void setCancanRefresh(boolean type) {
        refreshLayout.setCancanRefresh(type);
    }


    /**
     * 是否显示初始化动画
     * 默认不显示
     *
     * @return
     */
    public void DataRefresh() {

    }

    /**
     * 是否显示初始化动画
     * 默认不显示
     *
     * @return
     */

    public void DataRoadMore() {

    }

    /**
     * 下拉刷新成功回调
     */
    protected void finishRefresh() {
        // 结束刷新
        refreshLayout.finishRefresh();
    }

    /**
     * 上拉加载成功回调
     */
    protected void finishLoadMore(boolean type) {
        // 结束刷新
        refreshLayout.finishLoadMore();
        if (type) {
            refreshLayout.setCanLoadMore(false);
        } else {
            refreshLayout.setCanLoadMore(true);
        }
    }

    protected void Interface() {

    }

    /**
     * 显示加载中状态
     */
    protected void showLoading() {
        refreshLayout.showView(ViewStatus.LOADING_STATUS);

    }

    /**
     * 加载完成的状态
     */
    protected void showContentView() {
        refreshLayout.showView(ViewStatus.CONTENT_STATUS);

    }

    /**
     * 加载失败点击重新加载的状态
     */
    protected void showeMPTY(String text) {
        Logger.i("执行？？" + text);
        refreshLayout.showView(ViewStatus.EMPTY_STATUS);
        View error = refreshLayout.getView(ViewStatus.EMPTY_STATUS);
        empty_text = findView(error, R.id.empty_text);
        empty_text.setText(text);


    }

    /**
     * 网络显示错误
     */
    protected void showError() {
        // 结束刷新
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        refreshLayout.showView(ViewStatus.ERROR_STATUS);
        View error = refreshLayout.getView(ViewStatus.ERROR_STATUS);
        findView(error, R.id.shuax).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * 布局
     */
    public abstract int getLayout();
}


